{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mixed Mode S-Parameters & Impedance Transformation\n",
    "\n",
    "Mini-circuits [EP2C+](https://www.minicircuits.com/pdfs/EP2C+.pdf) is a 1.8 to 12.5 GHz MMIC based splitter/combiner. The s-parameters provided by Mini-circuits are single-ended.  For this example, the single-ended s-parameters will be converted to mixed mode s-parameters so that the common mode gain (the gain from the common port to the common mode terminated in 25 Ω) can be examined. Additionally, the differential mode gain (the gain from the common port to the differential mode terminated in 100 Ω) can be plotted. It is expected that the differential mode gain should be well below the common mode gain since this is a 0 degree splitter/combiner.\n",
    "\n",
    "Lastly, since it is desired to use this network in a cascade analysis as a 2-port block in a 50 Ω environment, the differential mode will be terminated in 100 Ω and a 50 Ω port transformed to 25 Ω will be connected to the common mode port:\n",
    "\n",
    "<img src=\"mixedmodeSandZtransform_files/mixed_mode.png\">"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "\n",
    "import skrf\n",
    "\n",
    "filename = r'mixedmodeSandZtransform_files/EP2C+_Plus25DegC_Unit1.S3P'\n",
    "se_ntwk = skrf.Network(filename)\n",
    "se_ntwk.frequency.unit = 'GHz'\n",
    "\n",
    "# plot single-ended s-parameters\n",
    "fig,ax0 = plt.subplots(1)\n",
    "se_ntwk.plot_s_db(ax=ax0)\n",
    "ax0.set_title(f'Single-Ended S-Parameters for {se_ntwk.name}')\n",
    "\n",
    "# use the same frequency list for all networks\n",
    "freq = se_ntwk.frequency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# convert to mixed-mode s-parameters\n",
    "mm_ntwk = se_ntwk.copy()\n",
    "\n",
    "# for a 3-port, the common port has to be the last\n",
    "mm_ntwk.renumber([0,1,2],[2,1,0])\n",
    "mm_ntwk.se2gmm(p=1)\n",
    "\n",
    "# make the differential mode port and common mode ports consistent with the figure above\n",
    "mm_ntwk.renumber([2,1,0],[0,2,1])\n",
    "\n",
    "fig,ax1 = plt.subplots(1)\n",
    "mm_ntwk.plot_s_db(1,0,label='Differential Mode Gain',ax=ax1)\n",
    "mm_ntwk.plot_s_db(2,0,label='Common Mode Gain',ax=ax1)\n",
    "ax1.set_title('Mixed Mode Gain')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a 25 to 50 ohm transformer\n",
    "\n",
    "# turns ratio\n",
    "n = np.sqrt(2)\n",
    "\n",
    "# ABCD Parameters\n",
    "a = np.array([[n, 0], [0, 1/n]])[np.newaxis, :, :]\n",
    "abcd = np.tile(a, [freq.npoints,1,1])\n",
    "\n",
    "transformer = skrf.Network(name='transformer',frequency=freq)\n",
    "transformer.s = skrf.a2s(abcd, z0=[50, 50])\n",
    "\n",
    "# connect the transformer to the common mode port\n",
    "# common mode port impedance transformed from 50 Ω to 25 Ω\n",
    "mm_ntwk_t = skrf.connect(mm_ntwk,2,transformer,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create a 100 Ω termination\n",
    "term = skrf.Network(name='100 ohm termination',z0=100,\n",
    "                    s=np.zeros([mm_ntwk.frequency.npoints]),\n",
    "                    frequency=mm_ntwk.frequency)\n",
    "\n",
    "# connect the 100 Ω termination to the differential mode port\n",
    "mm_ntwk_2port = skrf.connect(mm_ntwk_t,1,term,0)\n",
    "\n",
    "fig,ax2 = plt.subplots(1)\n",
    "mm_ntwk.plot_s_db(2,0,label='from 3-Port',ax=ax2,marker='o',lw=1,markersize=5)\n",
    "mm_ntwk_2port.plot_s_db(1,0,label='from 2-Port',ax=ax2)\n",
    "\n",
    "ax2.set_title('Common Mode Gain');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# compare the common mode gain of the impedance transformed 2-port to the mixed-mode untransformed 3-port\n",
    "complex_diff = np.abs(mm_ntwk_2port.s[:,1,0] - mm_ntwk.s[:,2,0])\n",
    "\n",
    "# don't give warning for -inf\n",
    "complex_diff[complex_diff==0] = np.nan\n",
    "\n",
    "fig,ax3 = plt.subplots(1)\n",
    "plt.plot(freq.f_scaled,20*np.log10(complex_diff))\n",
    "ax3.set_title('Complex Difference (dB): 2-port and 3-port')\n",
    "ax3.set_xlabel('Frequency (GHz)');"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
